home *** CD-ROM | disk | FTP | other *** search
- /*
-
- sdata.cpp
- 10-25-91
- Streamable Data: Loose Data Binder v 1.5
-
- Copyright 1991
- John W. Small
- All rights reserved
-
- PSW / Power SoftWare
- P.O. Box 10072
- McLean, Virginia 22102 8072 USA
-
- John Small
- Voice: (703) 759-3838
- CIS: 73757,2233
-
- */
-
- #include "sdata.hpp"
- #include <string.h>
-
-
- void SData::construct(voiD D, unsigned sizeofData,
- unsigned Did, int dup)
- {
- this->sizeofData = sizeofData;
- this->Did = Did;
- if ((this->dup = dup) == 0)
- this->D = D;
- else
- this->D = clone(D);
- if (!this->D)
- sizeofData = 0;
- }
-
- voiD SData::clone(const voiD D)
- {
- voiD cD = voiD0;
-
- if (sizeofData && D)
- if ((cD = new char[sizeofData]) != voiD0)
- memcpy(cD,D,sizeofData);
- return cD;
- }
-
- ostream& SData::store(ostream& os)
- {
- os << sizeofData << endm << Did << endm;
- if (!os)
- serror("unable to store SData"
- ": sizeofData and Did");
- else if (sizeofData) {
- os.write((const char *)D,sizeofData);
- if (!os)
- serror("unable to store"
- " SData data");
- }
- return os;
- }
-
- StreamablE SData::load(istream& is,
- StreamablE InstancE)
- {
- unsigned sizeofData, Did;
- voiD D = voiD0;
-
- if (!(is >> sizeofData >> nextm >> Did
- >> nextm)) {
- lserror("unable to load SData"
- ": sizeofData and Did",
- ID_CLASS);
- return StreamablE0;
- }
- if (sizeofData) {
- if ((D = new char[sizeofData])
- != voiD0) {
- is.read((char *)D,sizeofData);
- if (!is) {
- lserror("loading "
- "SData data",
- ID_CLASS);
- delete D;
- return StreamablE0;
- }
- }
- else {
- lserror("unable to allocate "
- "memory for SData data",
- ID_CLASS);
- is.ignore(sizeofData);
- return StreamablE0;
- }
- }
- if (!InstancE)
- if ((InstancE = (StreamablE)
- new SData
- (UNIQUE_STREAMABLE))
- == StreamablE0) {
- lserror("unable to construct"
- " SData",ID_CLASS);
- delete D;
- return StreamablE0;
- }
- ((SDatA)InstancE)->
- construct(D,sizeofData,Did,0);
- ((SDatA)InstancE)->dup = 1;
-
- return InstancE;
- }
-
- SData::SData(char * s, int dup)
- : Streamable(UNIQUE_STREAMABLE,ID_CLASS)
- {
- construct(s,(dup?(s?(strlen(s)+1):0):0),
- DID_String,dup);
- }
-